package com.ysq.ip.spider.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.jsoup.nodes.Element;

public class CommonUtil {

	/**
	 * 获得log4j logger 对象
	 * 
	 * @param clazz
	 * @return
	 */
	public static Logger getLogger(Class<?> clazz) {
		Logger logger = Logger.getLogger(clazz);
		return logger;
	}
	
	/**
	 * 将文本写入文件
	 * 
	 * @param path
	 * @param content
	 * @param append
	 */
	public static void writeFileContent(String path, String content, boolean append) {
		FileWriter fw = null;
		BufferedWriter writer = null;
		try {
			fw = new FileWriter(path, append);
			writer = new BufferedWriter(fw);

			writer.write(content);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				writer.close();
				fw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 获得 文本文件内容
	 * 
	 * @param path
	 * @return
	 */
	public static String getFileContent(String path) {
		StringBuilder builder = new StringBuilder();

		try {
			FileReader fr = new FileReader(path);
			BufferedReader reader = new BufferedReader(fr);
			String line = null;
			while ((line = reader.readLine()) != null) {
				builder.append(line);
				builder.append("\r\n");
			}

			reader.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return builder.toString();
	}
	
	/**
	 * 查询第一个并获取 element text 
	 * @param ele
	 * @param selector
	 * @return
	 */
	public static String selectElementText(Element ele, String selector){
		String text = null;
		
		Element selEle = ele.select(selector).first();
		
		if(selEle != null){
			text = selEle.text();
		}
		return text;
	}
	
	public static void main(String[] args) {
		
	}
	
}
